import { createApp,ref } from "vue";
import App from "./App.vue";
import router from "./router";
import ElementPlus from "element-plus";
import "element-plus/dist/index.css"; 
import "normalize.css";
import "@/assets/base.scss";
import { createPinia } from 'pinia'
 
import zhCN from 'element-plus/es/locale/lang/zh-cn';

const debounce = (fn, delay = 300) => {
    let timer = null;
    return (...args) => {
        clearTimeout(timer);
        timer = setTimeout(() => fn(...args), delay);
    };
};


const _ResizeObserver = window.ResizeObserver;
window.ResizeObserver = class ResizeObserver extends _ResizeObserver {
    constructor(callback) {
        callback = debounce(callback, 16);
        super(callback);
    }
};

const app = createApp(App);


// ② 再注册全局后置钩子
const cachedNames = ref([])
router.afterEach(to => { 
  to.matched
    .filter(r => r.meta?.keepAlive)
    .forEach(r => {
      const name = r.components?.default?.name
      if (name && !cachedNames.value.includes(name)) {
        cachedNames.value.push(name)
      }
    })
})
 
app.provide('cachedNames', cachedNames).use(createPinia()).use(router).use(ElementPlus,  { locale: zhCN }).mount("#app");

